package com.woniu.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniu.entity.Role;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author hyl
 * @since 2022-08-28
 */
@Repository
public interface RoleMapper extends BaseMapper<Role> {
    /**
     * 添加角色
     *
     * @param role 封装了角色信息的 Role 实例
     * @return
     */
    @Insert("insert into role (null,role_name,role_status) values (#{role.roleId},#{role.roleName},#{role.roleStatus})")
    void add(Role role);


    /**
     * 根据 id 逻辑删除角色
     *
     * @param id 角色的 id
     */
    @Delete("update role set status = 'n' where id = #{id}")
    void delete(int id);


    /**
     * 根据指定id查询角色信息
     *
     * @param id 角色的id
     * @return 根据 id 查询到的角色信息
     */
    @Select("select * from role where role_id = #{id}")
    @Results(id = "roleMap", value = {
            @Result(id = true, column = "role_id", property = "roleId"),
            @Result(column = "role_name", property = "roleName"),
            @Result(column = "role_status", property = "roleStatus"),
            // 角色权限明细
            @Result(column = "role_id", property = "authList", javaType = List.class,
                    many = @Many(select = "com.woniu.mapper.AuthMapper.getByRoleId", fetchType = FetchType.LAZY)),
            // 角色菜单明细
            @Result(column = "role_id", property = "menuList", javaType = List.class,
                    many = @Many(select = "com.woniu.mapper.MenuMapper.getByRoleId", fetchType = FetchType.LAZY)),
    })
    Role getById(int id);

    @Select("select count(*) from role where role_name = #{name}")
    int getByName(String name);

    @UpdateProvider(type = RoleSqlProvider.class, method = "update")
    void update(Role role);

    @SelectProvider(type = RoleSqlProvider.class, method = "select")
    @ResultMap("roleMap")
    List<Role> getByCondition(Role role);


    @Slf4j
    class RoleSqlProvider {
        /**
         * 得到动态修改的sql语句
         *
         * @param role 封装了修改条件的 Role 实例
         * @return 返回动态产生的sql语句
         */
        public String update(Role role) {
            return new SQL() {
                {
                    UPDATE("role");
                    if (role.getRoleName() != null && !role.getRoleName().equals("")) {
                        SET("role_name=#{role.roleName}");
                    }
                    if (role.getRoleStatus() != null) {
                        SET("role_status=#{role.roleStatus}");
                    }
                    WHERE("role_id=#{role.roleId}");
                }
            }.toString();
        }

        /**
         * 得到动态产生的多条件查询 sql 语句
         *
         * @param role 封装了查询条件的 Role 实例
         * @return 动态产生的 sql 查询语句
         */
        public String select(Role role) {
            return new SQL() {
                {
                    SELECT("*").FROM("role");
                    if (role.getRoleName() != null) {
                        WHERE("role_name=#{roleName}");
                    }
                    if (role.getRoleStatus() != null) {
                        WHERE("role_status=#{roleStatus}");
                    }
                }
            }.toString();
        }
    }
}
